package com.ihr360.authority.sharing.privilege;

import com.ihr360.authority.sharing.service.AuthUserResourceService;
import com.ihr360.commons.data.util.Pair;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.validation.ValidationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

public class BatchDeletePrivilege<ID> extends AbstractBizClassPrivilege<ID> {
    final Logger logger = LoggerFactory.getLogger(getClass());

    private List<ID> ids;

    public BatchDeletePrivilege(List<ID> ids, AuthUserResourceService authUserResourceService,IAccessControlClass iAccessControlClass) {
        super(iAccessControlClass);
        this.authUserResourceService = authUserResourceService;
        this.method = AccessControlMethod.METHODS.batchDelete;
        this.ids = ids;
    }

    public boolean isAccessControl() {
        return accessControl;
    }

    public void validate() {
        if (isAccessControl()) {
            ValidationUtils.rejectIfEmpty(userResourcesPrivilege, null, "您没有权限进行此操作");
            if (CollectionUtils.isNotEmpty(ids)) {
                for (ID id : ids) {
                    String strId = toStringId(id);
                    boolean containsKey = userResourcesPrivilege.containsKey(strId);
                    ValidationUtils.rejectIfNotEqual(containsKey, true, null, "您没有权限进行此操作");
                }
            }
        }
    }

    public BatchDeletePrivilege doValidate() {
        boolean noAccessControl = noAccessControl(this.method);
        privilege = null;
        if (!noAccessControl) {
            Pair<Map<Object, Integer>, Map<Object, Integer>> pair =
                    authUserResourceService.queryUserResourcesPrivilege(iAccessControlClass.getEntityClass(), iAccessControlClass.getIdClass());
            if (pair != null) {
                toIDKeyMap(pair.getFirst(), pair.getSecond());
            }
            validate();
        }
        this.accessControl = !noAccessControl;
        return this;
    }
}